<template>
  <div class="login_box">
    <div class="login_form">
      <!-- 表单头部 -->
      <div class="avater">
        <img src="@/img/avater.png" alt="" />
      </div>
      <!-- 表单区域 -->
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
        label-width="0px"
        class="form_box"
      >
        <!-- 用户名区域 -->
        <el-form-item prop="username">
          <el-input
            placeholder="请输入登录名称"
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
            clearable
          ></el-input>
        </el-form-item>
        <!-- 密码区域 -->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-button type="primary" @click="login">登陆</el-button>
        <el-button type="info" @click="resetForm">重置</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登陆密码", trigger: "blur" },
          { min: 3, max: 11, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    resetForm() {
      this.$refs.loginFormRef.resetFields();
      this.$Message.success({ message: "恭喜你,重置成功", center: true });
    },
    login() {
      this.$refs.loginFormRef.validate(async (valide) => {
        if (!valide) return;
        try {
          const { data: result } = await this.$http.post(
            "login",
            this.loginForm
          );
          if (result.meta.status != 200) {
            this.$Message.error({ message: "请求出错", center: true });
            return;
          }
          window.localStorage.setItem("token", result.data.token);
          this.$Message.success({ message: "恭喜你,登录成功", center: true });
          this.$router.push("home");
        } catch (error) {
            this.$Message.error({message:error.message,center:true})
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_box {
  height: 100%;
  width: 100%;
  background: #2b4b6d;
  .login_form {
    width: 400px;
    height: 375px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate3d(-50%, -50%, 0);
    border-radius: 3px;
    text-align: center;
    .avater {
      height: 100px;
      width: 100px;
      position: absolute;
      left: 50%;
      transform: translate3d(-50%, -50%, 0);
      border-radius: 50%;
      background: #eee;
      padding: 10px;
      box-shadow: 0 0 5px #fff;
      img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
      }
    }
    .form_box {
      position: absolute;
      top: 100px;
      height: 100%;
      width: 100%;
      box-sizing: border-box;
      padding: 10px;
    }
  }
}
</style>


